﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using Utility;

namespace HaiFang
{
    /// <summary>
    /// Summary description for UserInfoHandler
    /// </summary>
    public class UserInfoHandler : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            if (!String.IsNullOrEmpty(context.Request.QueryString["action"]))
            {
                string action = context.Request.QueryString["action"];
                switch (action)
                {
                    case "GetUserGroup":
                        {
                            string Params = context.Request.QueryString["Params"];
                            ResponseGetUserGroup(context, Params);
                        }
                        break;
                    case "GetUserInfo":
                        {
                            string Params = context.Request.QueryString["Params"];
                            ResponseGetUserInfo(context, Params);
                        }
                        break;
                    default:
                        WebHelper.WriteJsonError(context, "未知的操作.");
                        break;
                }
            }
            else if (context.Request.Form.Count > 0)
            {
                string action = context.Request.Form["action"];
                switch (action)
                {
                    case "AddUserInfo":
                        {
                            string Params = context.Request.Form["Params"];
                            if (!string.IsNullOrEmpty(Params))
                            {
                                ResponseAddUserInfo(context, Params);
                            }
                        }
                        break;
                    case "DelUserInfo":
                        {
                            string Params = context.Request.Form["Params"];
                            if (!string.IsNullOrEmpty(Params))
                            {
                                ResponseDelUserInfo(context, Params);
                            }
                        }
                        break;
                    case "SaveUserInfo":
                        {
                            string Params = context.Request.Form["Params"];
                            if (!string.IsNullOrEmpty(Params))
                            {
                                ResponseSaveUserInfo(context, Params);
                            }
                        }
                        break;
                    case "ModifyPassword":
                        {
                            string OldPassword = context.Request.Form["OldPassword"];
                            string Password = context.Request.Form["Password"];
                            if (!string.IsNullOrEmpty(OldPassword) && !string.IsNullOrEmpty(Password))
                            {
                                ResponseModifyPassword(context, OldPassword,Password);
                            }
                        }
                        break;
                    default:
                        WebHelper.WriteJsonError(context, "未知的操作");
                        break;
                }
            }
        }

        private void ResponseGetUserGroup(HttpContext context, string Params)
        {
            try
            {
                IList<UserGroup> list = ProxyUserGroup.QueryUserGroup();
                WebHelper.WriteJsonResult(context, list);
            }
            catch (Exception ex)
            {
                LogHelper.Writelog(LogType.LogType_Error, ex.Message, ex);
                WebHelper.WriteJsonError(context, null);
            }
        }

        private void ResponseGetUserInfo(HttpContext context, string Params)
        {
            try
            {
                IList<UserInfoWithGroup> list = ProxyUserInfo.QueryUserInfo(WebHelper.GetCurrentUserID(), 2, Convert.ToInt32(Params));
                WebHelper.WriteJsonResult(context, list);
            }
            catch (Exception ex)
            {
                LogHelper.Writelog(LogType.LogType_Error, ex.Message, ex);
                WebHelper.WriteJsonError(context, null);
            }
        }

        private void ResponseAddUserInfo(HttpContext context, string Params)
        {
            try
            {
                int ErrorMSG = -1;
                UserInfo r = null;
                if (!String.IsNullOrEmpty(Params))
                {
                    r = JsonHelper.JsonDeserialize<UserInfo>(Params);
                    if (r != null)
                    {
                        string Password = Security.Encrypt(r.Password);
                        ProxyUserInfo.InsertUserInfo(WebHelper.GetCurrentUserID(), 1, 0, r.GUID, r.GroupID, r.UserName, Password, Password, r.Description, out ErrorMSG);
                        WebHelper.WriteJsonResult(context, ErrorMSG);
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.Writelog(LogType.LogType_Error, ex.Message, ex);
            }
            WebHelper.WriteJsonError(context, null);
        }

        private void ResponseDelUserInfo(HttpContext context, string Params)
        {
            try
            {
                int ErrorMSG = -1;
                UserInfo r = null;
                if (!String.IsNullOrEmpty(Params))
                {
                    r = JsonHelper.JsonDeserialize<UserInfo>(Params);
                    if (r != null)
                    {
                        ProxyUserInfo.InsertUserInfo(WebHelper.GetCurrentUserID(), 3, r.AutoID, r.GUID, r.GroupID, r.UserName, r.Password, r.Password, r.Description, out ErrorMSG);
                        WebHelper.WriteJsonResult(context, ErrorMSG);
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.Writelog(LogType.LogType_Error, ex.Message, ex);
            }
            WebHelper.WriteJsonError(context, null);
        }

        private void ResponseSaveUserInfo(HttpContext context, string Params)
        {
            try
            {
                int ErrorMSG = -1;
                UserInfoWithPassword r = null;
                if (!String.IsNullOrEmpty(Params))
                {
                    r = JsonHelper.JsonDeserialize<UserInfoWithPassword>(Params);
                    if (r != null)
                    {
                        string Password = Security.Encrypt(r.Password);
                        ProxyUserInfo.InsertUserInfo(WebHelper.GetCurrentUserID(), 2, r.AutoID, r.GUID, r.GroupID, r.UserName, r.OldPassword, Password, r.Description, out ErrorMSG);
                        if (ErrorMSG == 0 && r.AutoID==WebHelper.GetCurrentUserID())
                        {
                            //修改当前用户密码
                            WebHelper.SetUserPassword(Password);
                        }
                        WebHelper.WriteJsonResult(context, ErrorMSG);
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.Writelog(LogType.LogType_Error, ex.Message, ex);
            }
            WebHelper.WriteJsonError(context, null);
        }

        private void ResponseModifyPassword(HttpContext context, string OldPassword,string Password)
        {
            try
            {
                int ErrorMSG = -1;
                SimpleUserInfo r = WebHelper.GetCurrentUserInfo();
                OldPassword = Security.Encrypt(OldPassword);
                Password = Security.Encrypt(Password);
                ProxyUserInfo.InsertUserInfo(WebHelper.GetCurrentUserID(), 2, r.AutoID, r.GUID, r.GroupID, r.UserName, OldPassword, Password, r.Description, out ErrorMSG);
                if (ErrorMSG == 0)
                {
                    WebHelper.SetUserPassword(Password);
                }
                WebHelper.WriteJsonResult(context, ErrorMSG);
                return;
            }
            catch (Exception ex)
            {
                LogHelper.Writelog(LogType.LogType_Error, ex.Message, ex);
            }
            WebHelper.WriteJsonError(context, null);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}